// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-12

package post

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/logics/master/ticket"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib"
)

// Logic
// 保存渔票.
type Logic struct {
	ticket.Logic

	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	affects     int64
	kind        kind.ArcadeAdminOperation
	kindContent string
	pond        *models.ArcadePond
	ticket      *models.Ticket

	request  *Request
	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: &Response{},
	}
}

// After
// 后置日志.
func (o *Logic) After(ctx context.Context, _ iris.Context, _ result.Result) {
	if o.affects > 0 {
		_, _ = services.NewArcadeLogsService().Add(ctx,
			o.arcade, o.admin, o.kind, o.kindContent,
		)
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Json(i, o.request); err == nil {
		o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)

	// 1. 事务处理.
	if err = db.Transaction(ctx,
		o.RunCheckTicket,
		o.RunCheckPond,
		o.RunSave,
		// o.RunStatistic,
	); err != nil {
		return res.WithError(err)
	}

	// 2. 保存完成.
	return res.With(o.response)
}

// RunCheckPond
// 检查鱼塘.
func (o *Logic) RunCheckPond(ctx context.Context, sess *db.Session) (err error) {
	var has bool

	// 1. 查询鱼塘.
	if o.pond, has, err = services.NewArcadePondService(sess).Dao.GetById(ctx, o.request.PondId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 无效鱼塘.
	//    不存在或已被删除.
	if !has || o.pond.ArcadeId != o.arcade.Id || o.pond.Deleted.IsYes() {
		err = errs.ErrArcadePondNotFound
	}
	return
}

// RunCheckTicket
// 检查渔票.
func (o *Logic) RunCheckTicket(ctx context.Context, sess *db.Session) (err error) {
	var has bool

	// 1. 新增模式.
	if o.request.Id == 0 {
		return
	}

	// 2. 查询渔票.
	if o.ticket, has, err = services.NewTicketService(sess).Dao.GetById(ctx, o.request.Id); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 3. 无效渔票.
	//    不存在或已被删除.
	if !has || o.ticket.ArcadeId != o.arcade.Id || o.ticket.Deleted.IsYes() {
		err = errs.ErrTicketNotFound
	}
	return
}

// RunSave
// 保存渔票.
func (o *Logic) RunSave(ctx context.Context, sess *db.Session) (err error) {
	if o.ticket != nil {
		return o.RunSaveUpdate(ctx, sess)
	}
	return o.RunSaveCreate(ctx, sess)
}

// RunSaveCreate
// 新增渔票.
func (o *Logic) RunSaveCreate(ctx context.Context, sess *db.Session) (err error) {
	var service = services.NewTicketService(sess)

	if o.ticket, err = service.Dao.AddByStruct(ctx, &models.Ticket{
		ArcadeId:     o.arcade.Id,
		State:        kind.TicketStateDisabled,
		Mode:         o.request.Mode,
		ModeStart:    o.request.ModeStart,
		ModeEnd:      o.request.ModeEnd,
		Time:         o.request.Time,
		TimeStart:    o.request.TimeStart,
		TimeEnd:      o.request.TimeEnd,
		PondId:       o.request.PondId,
		Seat:         o.request.Seat,
		SeatStrategy: o.request.SeatStrategy,
		Name:         o.request.Name,
		Hours:        o.request.Hours,
		Price:        o.request.Price,
		Quantity:     o.request.Quantity,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	if _, err = service.Dao.UpdateFieldsById(ctx, map[string]any{
		"deleted": kind.DeletedNo,
	}, o.ticket.Id); err != nil {
		err = errs.ErrDatabaseQuery
	}

	return
}

// RunSaveUpdate
// 更新渔票.
func (o *Logic) RunSaveUpdate(ctx context.Context, sess *db.Session) (err error) {
	return
}

// RunStatistic
// 重建统计.
func (o *Logic) RunStatistic(_ context.Context, sess *db.Session) (err error) {
	return
}
